﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QSTOOL.Codes;



namespace QSTOOL.Forms
{
    public partial class FormCommandMaintainModify : Form
    {
        //public int steps { get; set; }
        public string GUID { get; set; }
        public string ref_GUID { get; set; }
        public int type { get; set; }   //0:Append; 1:Insert After; 2:Insert Before
        public Command command { get; set; }
        public XMLTool xml { get; set; }
        public int operation { get; set; }  //0:Insert; 1:Modify

        public FormCommandMaintainModify()
        {
            InitializeComponent();
            this.type = 0;
        }

        public FormCommandMaintainModify(string GUID)
        { 
            
        }

        private void cbxCommand_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowTextBox(this.cbxCommand.Text);
        }

        private void ShowTextBox(string command)
        {
            this.txtDetail.Text = "";
            this.txtSource.Text = "";
            this.txtTarget.Text = "";
            this.txtDescription.Text = "";
            this.txtDetail.Enabled = false;
            this.txtSource.Enabled = false;
            this.txtTarget.Enabled = false;

            switch (command)
            { 
                case Command.COPY:
                    this.txtSource.Enabled = true;
                    this.txtTarget.Enabled = true;
                    break;
                case Command.DB2:
                case Command.DBF:
                case Command.FTP:
                case Command.CHECK:
                    this.txtDetail.Enabled = true;
                    break;
            }
        }

        private void FormCommandMaintainModify_Load(object sender, EventArgs e)
        {
            InitControls();
        }

        private void InitControls()
        {
            //Init the Command ComboBox
            this.cbxCommand.Items.Add(Command.DB2);
            this.cbxCommand.Items.Add(Command.DBF);
            this.cbxCommand.Items.Add(Command.COPY);
            this.cbxCommand.Items.Add(Command.CHECK);
            this.cbxCommand.Items.Add(Command.FTP);
            this.cbxCommand.SelectedIndex = 0;

            //If the GUID is null(Append), Show the Step ComboBox
            if (this.ref_GUID == null || this.ref_GUID == "")
            {
                this.lblStep.Visible = true;
                this.cbxStep.Visible = true;

                for (int i = 0; i < this.xml.allcommands.Count; i++)
                {
                    ComboBoxItem cbi = new ComboBoxItem();
                    cbi.Text = "Step " + (i + 1).ToString();
                    cbi.Value = i;
                    cbxStep.Items.Add(cbi);
                }
                cbxStep.SelectedIndex = 0;
            }

            //If modify, set the initial value to the controls
            if (this.operation == Operation.MODIFY)
            {
                this.cbxCommand.Text = this.command.command;
                this.txtDetail.Text = this.command.detail;
                this.txtSource.Text = this.command.source;
                this.txtTarget.Text = this.command.target;
                this.txtDescription.Text = this.command.description;
                this.txtGUID.Text = this.command.guid;

                this.lblStep.Visible = false;
                this.cbxStep.Visible = false;
                this.txtDescription.Focus();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Command cmd = new Command();
            cmd.command = this.cbxCommand.Text;
            cmd.detail = this.txtDetail.Text;
            cmd.source = this.txtSource.Text;
            cmd.target = this.txtTarget.Text;
            cmd.description = this.txtDescription.Text;
            if (this.txtGUID.Text == "")
            {
                cmd.guid = Guid.NewGuid().ToString();
            }
            else 
            {
                cmd.guid = this.txtGUID.Text;    
            }

            if (MessageBox.Show("确定要保存吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (this.operation == Operation.INSERT)
                {
                    Insert(cmd);
                }
                else
                {
                    Modify(cmd);
                }
                MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                FormCommandMaintain.Modified = true;
            }
            
            this.Close();
        }

        private void Insert(Command cmd)
        {
            switch (this.type)
            { 
                case Operation.APPEND:
                    int step = Convert.ToInt16(((ComboBoxItem)this.cbxStep.SelectedItem).Value);
                    this.xml.Insert(cmd, step);
                    break;
                case Operation.INSERT_AFTER:
                case Operation.INSERT_BEFORE:
                    this.xml.Insert(cmd, this.ref_GUID, this.type);
                    break;
            }
        }

        private void Modify(Command cmd)
        {
            this.xml.Modify(cmd);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
